#include "ProviderParameter.h"

#include "core/AppSignals.h"

ProviderParameter::ProviderParameter()
    : PropertyProvider()
{
}

void ProviderParameter::initEditor(
    QtTreePropertyBrowser* editor,
    shared_ptr<BaseElement> entity
)
{
    entity_ = dynamic_pointer_cast<uml::Parameter>(entity);
    propertyToId.clear();
    PropertyProvider::initEditor(editor, entity);

    addProperty("default2", "default", entity_->default2());
    QStringList directionNames;
    directionNames.append("in");
    directionNames.append("inout");
    directionNames.append("out");
    directionNames.append("return");
    addProperty("direction", "direction", static_cast<int>(entity_->direction()), directionNames);
    QStringList effectNames;
    effectNames.append("create");
    effectNames.append("read");
    effectNames.append("update");
    effectNames.append("delete");
    addProperty("effect", "effect", static_cast<int>(entity_->effect()), effectNames);
    addProperty("isException", "isException", entity_->isException());
    addProperty("isOrdered", "isOrdered", entity_->isOrdered());
    addProperty("isStream", "isStream", entity_->isStream());
    addProperty("isUnique", "isUnique", entity_->isUnique());
    addProperty("lower", "lower", entity_->lower());
    addProperty("upper", "upper", entity_->upper());

}

void ProviderParameter::valueChanged(QtProperty* property, bool value)
{
    QString id = propertyToId[property];
    if (id == "isException") {
        entity_->setIsException(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isOrdered") {
        entity_->setIsOrdered(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isStream") {
        entity_->setIsStream(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "isUnique") {
        entity_->setIsUnique(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}

void ProviderParameter::valueChanged(QtProperty* property, double value)
{
    QString id = propertyToId[property];
    
}

void ProviderParameter::valueChanged(QtProperty* property, int value)
{
    QString id = propertyToId[property];
    if (id == "direction") {
        entity_->setDirection(static_cast<ParameterDirectionKind>(value));
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "effect") {
        entity_->setEffect(static_cast<ParameterEffectKind>(value));
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "lower") {
        entity_->setLower(value);
        emit appSignals->onElementChanged(this, entity_);
    } else if (id == "upper") {
        entity_->setUpper(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}

void ProviderParameter::valueChanged(QtProperty* property, QString value)
{
    QString id = propertyToId[property];
    if (id == "default2") {
        entity_->setDefault2(value);
        emit appSignals->onElementChanged(this, entity_);
    }
}
